/**
 * 
 */
package registnet.courseDemand.extras;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.torque.Torque;
import org.apache.torque.TorqueException;

import registnet.dao.CourseView;
import registnet.dao.CourseViewPeer;

/**
 * @author Dexter Ryan Carrasco
 */
public class LectureSectionCounter {
	public static void main(final String args[]) {
		// initialize Torque properties
		try {
			Torque.init("Torque.properties");
		} catch (final TorqueException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

		final Map<String, List<String>> courseSectionsMap = new HashMap<String, List<String>>();
		final List<CourseView> courseList = CourseViewPeer.getSubjectsList();
		final File inFile = new File(
				"C:/Documents and Settings/Dexter Ryan Carrasco/Desktop/2ndsem2008_sections.csv");
		final File outFile = new File(
				"C:/Documents and Settings/Dexter Ryan Carrasco/Desktop/2ndsem2008_sectionCOUNT.csv");

		try {
			// use buffering, reading one line at a time
			// FileReader always assumes default encoding is OK!
			final BufferedReader input = new BufferedReader(new FileReader(
					inFile));
			// use buffering

			try {
				String line = null; // not declared within while loop
				String tokens[];

				/*
				 * readLine is a bit quirky : it returns the content of a line
				 * MINUS the newline. it returns null only for the END of the
				 * stream. it returns an empty String if two newlines appear in
				 * a row.
				 */
				input.readLine();
				while ((line = input.readLine()) != null) {
					tokens = line.split(",");

					final String subject = tokens[1];
					final String sectionToken[] = tokens[0].split("-");
					final String lecSection = sectionToken[0];

					if (courseSectionsMap.containsKey(subject)) {
						final List<String> sectionList = courseSectionsMap
								.get(subject);
						if (!sectionList.contains(lecSection)) {
							sectionList.add(lecSection);
						}

					} else {
						final List<String> sections = new ArrayList<String>();
						sections.add(lecSection);
						courseSectionsMap.put(subject, sections);
					}

				}
			} finally {
				input.close();

			}
		} catch (final IOException ex) {
			ex.printStackTrace();
		}
		try {
			// use buffering, reading one line at a time
			// FileReader always assumes default encoding is OK!
			final BufferedWriter output = new BufferedWriter(new FileWriter(
					outFile));
			// use buffering

			try {
				output.write("Course,SectionCount");
				output.newLine();
				for (final Iterator<CourseView> courseIter = courseList
						.iterator(); courseIter.hasNext();) {
					final CourseView course = courseIter.next();
					final List<String> sections = courseSectionsMap.get(course
							.getCourseCode());
					int sectionCount;

					if (sections == null) {
						sectionCount = -1;
					} else {
						sectionCount = sections.size();
					}

					output.write(course.getCourseCode() + "," + sectionCount);
					output.newLine();
				}
			} finally {
				output.close();

			}
		} catch (final IOException ex) {
			ex.printStackTrace();
		}
	}
}
